package com.wenjiandaorudaochu.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wenjiandaorudaochu.demo.entity.User;
import com.wenjiandaorudaochu.demo.mapper.UserMapper;
import com.wenjiandaorudaochu.demo.service.UserService;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

/**
 * Created with IntelliJ IDEA
 *
 * @author liumao
 * @date 2023/4/3/003
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public Integer insertExcel() throws IOException {
        List<User> all = userMapper.findAll();
//        Integer countDb = userMapper.count();
//        System.out.println(countDb);
//        List<String> strings = userMapper.countClomns();
//        strings.forEach(i-> System.out.println(i));
        HSSFWorkbook wk = new HSSFWorkbook();
        // 创建一张食物信息表
        HSSFSheet sheet = wk.createSheet("食物信息表");
        // 创建标题行
        HSSFRow smallTitle = sheet.createRow(0);
        smallTitle.createCell(0).setCellValue("名字");
        smallTitle.createCell(1).setCellValue("年龄");
        smallTitle.createCell(2).setCellValue("性别");
        int count = 1;
        for (User excel : all) {
            HSSFRow row = sheet.createRow(count++);
            row.createCell(0).setCellValue(excel.getName());
            row.createCell(1).setCellValue(excel.getAge());
            row.createCell(2).setCellValue(excel.getGender());
            OutputStream outputStream = new FileOutputStream("D:\\food.xls");
            wk.write(outputStream);
            wk.close();
        }
        return 1;
    }

    @Override
    public User getUserInfo(String id) {
        return userMapper.selectById(id);

    }
}